var orderVm;
var creatOrderHttp;
var token='';
var openId='';
var unionId='';
var action='';//1代表学校订单
$(function(){

    token = getParam('token');
    openId = getParam('openId');
    action = getParam('action');
    window.localStorage.setItem("token",token);
    window.localStorage.setItem("openId",openId);

    orderVm = new Vue({
        el:'#order_id',
        data:{
            actId:'3',//活动ID
            payType:'2',//支付类型 1银联云闪付 2银联微信支付
            openId:openId,//openId
            unionId:unionId,//unionId
            userName:'',//姓名
            userPhone:'',//手机号
            longitude:'',//经度
            latitude:'',//纬度
            region:'',//所属地区
            address:'',//地址
            postCode:'',//邮政编号
            price:'140.00',//原价
            number:'1',//购买数量
            prePrice:'110.00',//优惠价
            pickerShow:false,
            addressColumns:adressPicker,
            isShowCalendar:false,
            minDate: getBADateObj("day",2),
			takeTicketDate:''
        },
        methods:{
            init:function(){
                if(action==1){
                }
            },
            onClosePage:function(){
                // window.open("about:blank", "_top").close();
                window.close();
            },
            deductPrice:function(){
                return _toFixed(Number(this.price) - Number(this.prePrice),2);
            },
            formatDate:function(date) {
                var monthStr= date.getMonth() + 1;
                var dateStr= date.getDate();
                monthStr=monthStr < 10 ? '0'+monthStr:monthStr;
                dateStr=dateStr < 10 ? '0'+dateStr:dateStr;
                return date.getFullYear()+'-'+ monthStr +'-' +dateStr;
            },
            onShowAddressPicker:function(){
                this.pickerShow=true;
            },
            onAddressConfirm:function(value, index) {
                if(value){
                    if(value.length==1){
                        this.region=value[0]
                    }
                    else if(value.length==2){
                        this.region=value[0]+value[1];
                    }
                    else if(value.length>=3){
                        this.region=value[0]+value[1]+value[2];
                    }

                }
            },
            onShowCalendar:function(){
                this.isShowCalendar=true;
            },
            onDateConfirm:function(date){
                this.isShowCalendar=false;
                this.takeTicketDate = this.formatDate(date);
            }
        }
    });
    orderVm.init();
});

creatOrderHttp=function(){

}

function placeOrder(){
    if(orderVm.userName==undefined || orderVm.userName==null || orderVm.userName==''){
        vant.Toast.fail('请输入姓名');
    }
    else if(orderVm.userPhone==undefined || orderVm.userPhone==null || orderVm.userPhone==''){
        vant.Toast.fail('请输入手机号');
    }
    else if(!checkPhone(orderVm.userPhone)){
        vant.Toast.fail('请输入正确的手机号码');
    }
    else if((orderVm.region==undefined || orderVm.region==null || orderVm.region=='')){
        vant.Toast.fail('请输入所属地区');
    }
    else if((orderVm.address==undefined || orderVm.address==null || orderVm.address=='')){
        vant.Toast.fail('请输入地址');
    }else if(orderVm.postCode != undefined && orderVm.postCode != null && orderVm.postCode != '' && orderVm.postCode.length != 6){
        vant.Toast.fail('邮政编码请输入6位数字');
    }else if((orderVm.takeTicketDate==undefined || orderVm.takeTicketDate==null || orderVm.takeTicketDate=='')){
        vant.Toast.fail('请输入预约取票日期');
    }else{
        showVanLoading('下单中...');
        //var jsonData = {"name":orderVm.userName,"mobile":orderVm.userPhone};
        //alert(openId + " : " + orderVm.openId);
        var param="name="+orderVm.userName+"&mobile="+orderVm.userPhone +"&actId=" + orderVm.actId + "&openId=" + openId + "&unionId=" + unionId + "&payType=" + orderVm.payType + "&number=" + orderVm.number + "&longitude="+orderVm.longitude + "&latitude=" + orderVm.latitude;
        param = param + "&region="+orderVm.region + "&address=" + orderVm.address + "&postCode=" + orderVm.postCode + "&takeTicketDate=" + orderVm.takeTicketDate;

        _getHttpsData('/lyPay/placeOrderNow',param,
            function(res){
                closeVanLoading();
                if(res.status){
                    var orderId=res.data;
                    window.location='../../html/order/wanda_pay.html?orderId='+orderId+'&token='+ token+'&openId='+openId+'&phone='+orderVm.userPhone;
                }else{
                    vant.Toast.fail(res.msg);
                }
            },
            function(err){
                closeVanLoading();
                vant.Toast.fail("网络异常"+err.readyState+":"+err.statusText+"("+err.status+")");
            }
        );
    }
    
}